layui.use(['jquery'], function () {
    var $ = layui.jquery;

    $("#username").click(function (){
        layer.tips('请输入用于登录的账号,不建议使用中文', '#username',{
            tips:[2,'#000000']
        });
    })
    $("#email").click(function (){
        layer.tips('填写用于接收验证码的邮箱', '#email',{
            tips:[2,'#000000']
        });
    })
    /*获取验证码*/
    $("#registerCode").click(function (){
        var index = top.layer.msg('正在发送验证码，请稍候', {icon: 16, time: false, shade: 0.8});
        var username = $("#username").val();
        var email = $("#email").val();
        var password = $("#password").val();

        if (username == null || username == ""){
            layer.close(index);
            layer.msg("用户名为空!",{icon:2,time:1000});
        }else if (email == null || email == ""){
            layer.close(index);
            layer.msg("邮箱为空!",{icon:2,time:1000});
        }else if (password == null || password == ""){
            layer.close(index);
            layer.msg("密码为空!",{icon:2,time:1000});
        }else {
            $.ajax({
                type: "post",
                data: {
                    "username" : username,
                    "email" : email,
                    "password" : password,
                    "type": "getCode"
                },
                url: "/user/registerCode",
                success: function (res){
                    if (res.state == true){
                        layer.close(index);
                        layer.msg("验证码已发送，请注意接收")
                    }else if (res.state == false){
                        layer.close(index);
                        layer.msg(res.msg);
                    }
                }
            })
        }
    })

    /*注册按钮点击事件*/
    $("#register-btn").click(function (){
        var index = top.layer.msg('正在注册', {icon: 16, time: false, shade: 0.8});
        var username = $("#username").val();
        var email = $("#email").val();
        var password = $("#password").val();
        var code = $("#code").val();
        if (username == null || username == ""){
            layer.close(index);
            layer.msg("用户名为空!",{icon:2,time:1000});
        }else if (email == null || email == ""){
            layer.close(index);
            layer.msg("邮箱为空!",{icon:2,time:1000});
        }else if (password == null || password == ""){
            layer.close(index);
            layer.msg("密码为空!",{icon:2,time:1000});
        }else if (code == null || code == ""){
            layer.close(index);
            layer.msg("验证码为空!",{icon:2,time:1000});
        }else{
            $.ajax({
                type : "post",
                data: {
                    "username" : username,
                    "password" : password,
                    "email" : email,
                    "code" : code
                },
                url: "/user/register",
                success: function (res){
                    if (res.state == true){
                        layer.close(index);
                        layer.msg("注册成功");
                        location.href = "/login";
                    }else if (res.state == false){
                        layer.close(index);
                        layer.msg(res.msg);
                    }
                }
            })
        }
    })
});